/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucue.ptc.gco.ui;

import edu.ucue.ptc.gco.mdl.Lote;
import edu.ucue.ptc.gco.mdl.Producto;
import edu.ucue.ptc.gco.ngc.LoteManagerLocal;
import edu.ucue.ptc.gco.util.AtributoInvalidoException;
import edu.ucue.ptc.gco.util.ElementoYaExisteException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Marcelo
 */
@Named(value = "loteAction")
@SessionScoped
public class LoteAction implements Serializable {

    private Lote lote;
    private Producto productoSel;
    @Inject
    private LoteManagerLocal loteManager;
    private boolean modoModificar;

    @PostConstruct
    public void init() {
        lote = new Lote();
        lote.setFechaCaducidad(Calendar.getInstance().getTime());
    }

    public LoteAction() {
    }

    public static LoteAction getInstance() {
        ELContext context = FacesContext.getCurrentInstance().getELContext();
        ValueExpression ex = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
                createValueExpression(context, "#{loteAction}", LoteAction.class);
        return (LoteAction) ex.getValue(context);
    }

    public String modificar(Lote loteMod) {
        modoModificar = true;
        lote = loteMod;
        productoSel = loteMod.getProducto();
        return "lote";
    }

    public String seleccionar(Producto prodSel) {
        if (prodSel != null) {
            System.out.println("nom: " + prodSel.getNombreProducto());
        }
        return "";
    }
    
    public String actualizaLotes(Producto p) {
        RequestContext.getCurrentInstance().update("tablaP");
        return "";
    }
    public String guardar() {
        try {
            if (productoSel == null) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "Se debe elegir un producto"));
                return "";
            }
            lote.setProducto(productoSel);

            if (modoModificar) {
                loteManager.actualizarLote(lote, LoginAction.getUsername(), LoginAction.getPassword());                
            } else {
                loteManager.crearLote(lote, LoginAction.getUsername(), LoginAction.getPassword());
            }
            ProductoAction.getInstance().cargaLista();
            RequestContext.getCurrentInstance().update("tablaP");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "Guardado"));
            return "listaproductos"; 
        } catch (AtributoInvalidoException ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, ex.getMessage()));
            Logger.getLogger(LoteAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ElementoYaExisteException ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "El lote ya existe"));
            Logger.getLogger(LoteAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "Error"));
            Logger.getLogger(LoteAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }

    public String cancel() {
        return "index";
    }

    public Producto getProductoSel() {
        return productoSel;
    }

    public void setProductoSel(Producto productoSel) {
        this.productoSel = productoSel;
    }

    public Lote getLote() {
        return lote;
    }

    public void setLote(Lote lote) {
        this.lote = lote;
    }

    public boolean isModoModificar() {
        return modoModificar;
    }

    public void setModoModificar(boolean modoModificar) {
        this.modoModificar = modoModificar;
    }
}
